package cn.jjxx.modules.workflow.service.impl;

import java.util.Date;
import java.util.List;

import cn.jjxx.core.common.service.impl.CommonServiceImpl;
import cn.jjxx.core.query.wrapper.EntityWrapper;
import cn.jjxx.core.utils.ObjectUtils;
import cn.jjxx.core.utils.StringUtils;
import cn.jjxx.modules.activiti.service.IFlowService;
import cn.jjxx.modules.activiti.workflow.history.HistoryTaskComment;
import cn.jjxx.modules.workflow.mapper.ActSignTemplateMapper;
import cn.jjxx.modules.workflow.entity.ActSignTemplate;
import cn.jjxx.modules.workflow.service.IActSignTemplateService;

import org.activiti.engine.history.HistoricProcessInstance;
import org.framework.customutil.DateUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**   
 * @Title: 流程签字模板
 * @Description: 流程签字模板
 * @author zcg
 * @date 2018-04-17 21:25:15
 * @version V1.0   
 *
 */
@Transactional
@Service("actSignTemplateService")
public class ActSignTemplateServiceImpl  extends CommonServiceImpl<ActSignTemplateMapper,ActSignTemplate> implements  IActSignTemplateService {

	private static final String MESSAGE_STR = "_message";//消息
	
	private static final String DATE_STR = "_date";//日期
	
	private static final String ASSIGNEE_STR = "_assignee";//处理人
	
	private static final String FIRST_SIGN = "${";//前面符号
	
	private static final String LAST_SIGN = "}";//前面符号
	
	@Override
	public boolean save(ActSignTemplate actSignTemplate) {
		try {
			EntityWrapper<ActSignTemplate> wrapper = new EntityWrapper<ActSignTemplate>(ActSignTemplate.class);
	    	wrapper.eq("preDefId", actSignTemplate.getPreDefId());
	    	if(StringUtils.isNotEmpty(actSignTemplate.getOrgId())){
	    		wrapper.eq("orgId", actSignTemplate.getOrgId());
	    	}
	    	if(StringUtils.isNotEmpty(actSignTemplate.getMenuId())){
	    		wrapper.eq("menuId", actSignTemplate.getMenuId());
	    	}
	    	ActSignTemplate obj = this.selectOne(wrapper);
	    	if(!ObjectUtils.isNullOrEmpty(obj)&&StringUtils.isNotEmpty(obj.getPreDefId())){
	    		this.delete(wrapper);
	    		this.insert(actSignTemplate);
	    	}else{
	    		this.insert(actSignTemplate);
	    	}
	    	return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}

	@Override
	public String getSignTemplate(String billId,IFlowService flowService) {
		HistoricProcessInstance hisProInst = flowService.getHisProInst(billId);
		String template = getSignTemplate(hisProInst);
		return template;
	}

	@Override
	public String replaceSignContents(String billId, IFlowService flowService) {
		HistoricProcessInstance hisProInst = flowService.getHisProInst(billId);
		String template = getSignTemplate(hisProInst);
		if(template!=null){
			List<HistoryTaskComment> comments = flowService.findUniqueCommentToTask(billId);
			for(HistoryTaskComment comment:comments){
				String content = comment.getTaskDefinitionKey()+MESSAGE_STR;
				String userName = comment.getTaskDefinitionKey()+ASSIGNEE_STR;
				String endTime = comment.getTaskDefinitionKey()+DATE_STR;
				Date date = comment.getOperateTime();
				if(template.contains(content)&&!ObjectUtils.isNullOrEmpty(endTime)){
					template= template.replace(FIRST_SIGN+content+LAST_SIGN, comment.getMessage());
				}
				if(template.contains(userName)&&!ObjectUtils.isNullOrEmpty(endTime)){
					template = template.replace(FIRST_SIGN+userName+LAST_SIGN, comment.getUserName());
				}
				if(template.contains(endTime)&&!ObjectUtils.isNullOrEmpty(date)){
					template = template.replace(FIRST_SIGN+endTime+LAST_SIGN,DateUtils.date2Str(date,DateUtils.date_sdf));
				}
			}
			while(template.contains(FIRST_SIGN)&&template.contains(LAST_SIGN)){
				String str = template.substring(template.indexOf(FIRST_SIGN),template.indexOf(LAST_SIGN)+1);
				template = template.replace(str,"");
			}
		}
		return template;
	}
	
	private String getSignTemplate(HistoricProcessInstance hisProInst){
		if (hisProInst!=null) {
			String preDefId = hisProInst.getProcessDefinitionId();
			EntityWrapper<ActSignTemplate> wrapper = new EntityWrapper<ActSignTemplate>(ActSignTemplate.class);
			wrapper.eq("preDefId", preDefId);
			ActSignTemplate ast = this.selectOne(wrapper);
			if(ast!=null){
				return ast.getSignTemplate();
			}
		}
		return null;
	}

}
